#include <stdio.h>
#include "tst/test_frame.h"

#define TEST_REPORT_SUCCESS() printf("\033[;34m[OK]\033[0m\n")
#define TEST_REPORT_FAILED() printf("\033[;31m[*FAILED*]\033[0m\n");
#define PRINTF_FLUSH(...) printf(__VA_ARGS__);fflush(stdout);
#define DELIMIT "------------------------------------------------------------------------------\n"

static void printReport(const T_TestCase *taTestCase, size_t nTestCaseCount);

int main(int argc, char *argv[])
{

    PRINTF_FLUSH("[START INIT]");
    if (gpfnInit != NULL && gpfnInit() != 0)
    {
        TEST_REPORT_FAILED();
        return -1;
    }
    else
    {
        TEST_REPORT_SUCCESS();
    }

    for (size_t i = 0; i < gnTestCaseCount; ++i)
    {
        PRINTF_FLUSH(DELIMIT);
        PRINTF_FLUSH("[START CASE][%04zd][%s]\n", i+1, gtaTestCase[i].caDesc);

        if ((gtaTestCase[i].iTestReuslt = (gtaTestCase[i].pfnTestCase)()) != 0)
        {
            TEST_REPORT_FAILED();
        }
        else
        {
            TEST_REPORT_SUCCESS();
        }
    }

    PRINTF_FLUSH(DELIMIT);
    PRINTF_FLUSH("[START CLEANUP]");
    if (gpfnCleanup != NULL && gpfnCleanup() != 0)
    {
        TEST_REPORT_FAILED();
        return -1;
    }
    else
    {
        TEST_REPORT_SUCCESS();
    }

    printReport(gtaTestCase, gnTestCaseCount);
}

static void printReport(const T_TestCase *taTestCase, size_t nTestCaseCount)
{
    size_t nSuccessCnt = 0;
    size_t nFailedCnt = 0;

    for (size_t i = 0; i < nTestCaseCount; ++i)
    {
        if (taTestCase[i].iTestReuslt != 0)
        {
            ++nFailedCnt;
        }
        else
        {
            ++nSuccessCnt;
        }
    }
    printf("\n[SUMMARY] SUCCESS CASE CNT[%zd], FAILED CASE CNT[%zd]\n", nSuccessCnt, nFailedCnt);

    if (nFailedCnt != 0)
    {
        printf("[FAILED CASES ARE]");
        for (size_t i = 0; i < nTestCaseCount; ++i)
        {
            if (taTestCase[i].iTestReuslt != 0)
            {
                printf(" %04zd", i + 1);
            }
        }
        printf("\n\n");
    }
}
